(function(undefined){
    var city_obj = {};
    
    city_obj.config = {
        url: '/handle.php?h=city/list',
		title_pro: "请选择省份",
		title_city: "请选择区县",
		title_dis: "请选择地区",
		defaultpro: 0,
		defaultcity: 0,
		defaultdis: 0,
		rank: 2
    }
    
    city_obj.obj = {
        province: null,
        city: null,
        district: null 
    }
    //初始化
    city_obj.init = function(param){
				
		for(i in param){
			city_obj.config[i] = param[i];
		}
		 
		
		
        city_obj.obj.province = $("#city_pro");
		city_obj.obj.city = $("#city_city");
		if(city_obj.config.rank == 3){
			city_obj.obj.dis = $("#city_dis");
		}
		
		
		city_obj.obj.province.bind("change", city_obj.pro_change);
		if (city_obj.config.rank == 3) {
			city_obj.obj.city.bind("change", city_obj.city_change);
		}
		
		
		
		
		city_obj.pro_init();	//初始化省份
		
    }
	
	//省份改变函数
	city_obj.pro_change = function(){
		var val = city_obj.obj.province.val();
		//去掉第三个的内容
		if (city_obj.config.rank == 3) {
			html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, []);
		}
		if(!val){
			html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city, []);			
		}else{
			city_obj.get_data(val, city_obj.set_city);
		}
		
		
	}   
	
	
	city_obj.city_change = function(){
		var val = city_obj.obj.city.val();
		if(!val){
			html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, []);
		}else{
			city_obj.get_data(val, city_obj.set_dis);
		}
	}
	
	
	//初始化省份
	city_obj.pro_init = function(){		
		city_obj.get_data(0, city_obj.set_pro);
		//如果设置了城市的默认值,则要初始化城市
		if(city_obj.config.defaultpro){
			city_obj.get_data(city_obj.config.defaultpro, city_obj.set_city);
		}else{
			//没有设置默认城市，
			html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city);
		}
		if (city_obj.config.rank == 3) {
			if (city_obj.config.defaultpro && city_obj.config.defaultcity) {
				city_obj.get_data(city_obj.config.defaultcity, city_obj.set_dis);
			}
			else {
				html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis);
			}
		}
		
		
		
	} 
	
	//设置城市数据
	city_obj.set_city = function(data){
		html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city, data, "id", "title", city_obj.config.defaultcity);
	}
	
	//设置城市数据
	city_obj.set_pro = function(data){
		html_obj.form.create_option(city_obj.obj.province, city_obj.config.title_pro, data, "id", "title", city_obj.config.defaultpro);
	}
	
	//设置地区数据
	city_obj.set_dis = function(data){
		html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, data, "id", "title", city_obj.config.defaultdis);
	}	
    
    
    //得到数据
    city_obj.get_data = function(pid, handle){
        $.getJSON(city_obj.config.url, {'pid':pid}, function(data){
            handle(data.msg);
        })
    }
    
    
    if(window.city_obj == undefined){
        window.city_obj = city_obj;
    }
})();